package com.java.mybatis.controller;

import com.java.mybatis.entity.*;
import com.java.mybatis.service.GoodsService;
import com.java.mybatis.service.OrderService;
import com.java.mybatis.service.OrdergoodsService;
import com.java.mybatis.service.PutinlocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (Order)表控制层
 *
 * @author makejava
 * @since 2021-01-15 17:10:28
 */
@Controller
@RequestMapping("/order/")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;
    @Autowired
    private OrdergoodsService ordergoodsService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private PutinlocationService putinlocationService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Order selectOne(Integer id) {
        return this.orderService.queryById(id);
    }

    /**
     * 查询order表的数据
     * @param session
     * @param map
     * @param model
     * @param page
     * @return
     */
     @RequestMapping("selectOrder")
     public String selectOrder(HttpSession session, Map map , Model model, Integer page) {
         Integer count= null;//返回受影响的行数
         try {
             count = orderService.orderCount();
         } catch (Exception e) {
             e.printStackTrace();
         }
         List<Order> orders = null;
         try {
             orders = orderService.GetAll((page-1)*2,2);
         } catch (Exception e) {
             e.printStackTrace();
         }
         System.out.println(count);
         //总共有多少页
         int goodPageMax = count % 2 == 0 ? count / 2 : count / 2 + 1;
         model.addAttribute("pageText","当前第"+page+"页,共"+goodPageMax+"页");
         //存入总共有多少页
         model.addAttribute("goodPageMax",goodPageMax);
         //分页当前页
         session.setAttribute("thisPage",page);

         model.addAttribute("count",count);

         model.addAttribute("selectOrders", orders);
         return "System/echart-2";
     }

    /**
     * 模糊查询
     * @param model
     * @param str
     * @return
     * @throws Exception
     */
     @RequestMapping("selectOrderlike")
     public String selectOrderlike(Model model, String str) throws Exception{
         List<Order> orders = orderService.selectOrderlike("%"+str+"%");
         model.addAttribute("selectOrders",orders);
         
         //模糊查询的中条数
         Integer  i = orderService.selectOrderlikeCount("%" + str + "%");
         System.out.println(i);
         model.addAttribute("count",i);
         return "System/echart-2";
     }

    /**
     * 添加数据
     * @param session
     * @param orderId
     */
     @RequestMapping("updateOrderState")
    public void updateOrderState(HttpSession session,Integer orderId){
        update(session,orderId);
     }
     //辅助方法
     private void update(HttpSession session,Integer orderId){
         //确认接货
         //1：修改order的state=1
         int i = orderService.updateOrderState(orderId);
         //2：修改ordergoods的ordergoods_state为1
         List<Ordergoods> list = ordergoodsService.queryByOrderId(orderId);
         for (Ordergoods ordergoods : list) {
             //ordergoods.getOrdergoodsId()  ordergoods表的主键   修改ordergoods的状态
             int i1 = ordergoodsService.updateOrdergoodsState(ordergoods.getOrdergoodsId());


             //给putinlocation表赋值
             Integer queryByMaxId = putinlocationService.queryByMaxId();  //查询最大id
             Putinlocation putinlocation = new Putinlocation();
             putinlocation.setPutinlocationId(queryByMaxId+1);
             putinlocation.setPutinlocationNumber("#lk120"+(queryByMaxId+1));
//             Goods goods1 = new Goods();
//             goods1.setGoodsId(ordergoods.getOrdergoodsId()+1);
             Ordergoods ordergoods1 = new Ordergoods();
             ordergoods1.setOrdergoodsId(ordergoods.getOrdergoodsId()+1);
             putinlocation.setOrdergoods(ordergoods1);
             Employees emp = (Employees) session.getAttribute("emp");
             putinlocation.setEmployees(emp);
             putinlocation.setPutinlocationState(0);
             putinlocation.setPutinlocationCode(1);
             putinlocation.setPutinlocationTime("");
             putinlocation.setPutinlocationMark("备用");
             putinlocationService.insert(putinlocation);

         }
     }
}
